﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using AnguaRPGEngine.Library.Systems;
using AnguaRPGEngine.Library.Configuration;
using AnguaRPGEngine.Library.Events;
using AnguaRPGEngine.Library.Events.Messages;

namespace AnguaRPGEngine.Library.Forms
{
    public partial class DiceTower : Form, IHandleMessagesOfType<ParentFormMoveEvent>
    {
        private Rectangle ParentRect = new Rectangle();
        public DiceTower(Form parent)
        {
            ParentRect = new Rectangle(parent.Left, parent.Top, parent.Width, parent.Height);
            InitializeComponent();
        }
       
        private void DiceTower_Load(object sender, EventArgs e)
        {
            FormPosition fp = null;
            try
            {
                fp = GameSystem.Instance.ClientConfig.FormPositions.Find(p => p.FormName == this.Name);
            }
            catch
            {
                fp = null;
            }
            if (fp == null)
            {
                this.Left = 0;
                this.Top = ParentRect.Top + ParentRect.Height + 30;
                fp = new FormPosition();
                fp.FormName = this.Name;
                fp.Location = this.Location;
                fp.Size = this.Size;
                fp.Visible = this.Visible;
                GameSystem.Instance.ClientConfig.FormPositions.Add(fp);
            }
            else
            {
                this.Location = fp.Location;
                this.Size = fp.Size;
                this.Visible = fp.Visible;
            } 
            diceBag1.Initialize();
        }


        private void DiceTower_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!GameSystem.Instance.IsShuttingDown)
            {

                e.Cancel = true;
                this.Visible = false;
            }
            else
            {
                FormPosition fp = null;
                try
                {
                    fp = GameSystem.Instance.ClientConfig.FormPositions.Find(p => p.FormName == this.Name);
                }
                catch
                {
                    fp = null;
                }
                if (fp == null)
                {

                    fp = new FormPosition();
                    fp.FormName = this.Name;
                    fp.Location = this.Location;
                    fp.Size = this.Size;
                    fp.Visible = this.Visible;
                    GameSystem.Instance.ClientConfig.FormPositions.Add(fp);
                }
                else
                {
                    fp.Location = this.Location;
                    fp.Size = this.Size;
                    fp.Visible = this.Visible;
                }
            }
        }

        #region IHandleMessagesOfType<ParentFormMoveEvent> Members

        public void HandleMessage(ParentFormMoveEvent message)
        {
            // need to be able to save pinned boolean to determine if this actually happens and if so, how
            // since right now it simply will jump to the right hand side of the parent window.
            this.Top = message.Location.Y + message.Size.Height + 2;
            this.Left = message.Location.X + message.Size.Width - this.Width;
        }

        #endregion
    }
}
